package com.icbc.open.chengdu.demo.account.bindingunbinding;

import com.csii.pp.icbc.util.MD5Util;
import com.csii.pp.icbc.util.Util;
import com.icbc.api.DefaultIcbcClient;
import com.icbc.api.internal.util.internal.util.fastjson.JSON;
import com.icbc.api.request.SettlementAccountBindingRequestV1;
import com.icbc.api.response.SettlementAccountBindingResponseV1;
import com.icbc.open.chengdu.demo.BaseData;
import com.icbc.open.chengdu.demo.utils.SaveFileUtils;
import com.icbc.open.chengdu.demo.utils.GenerateKeyUtils;
import com.icbc.open.chengdu.demo.utils.SecretUtils;

/**
 * 结算账户绑卡维护
 * 提供合作方将我行II、III类个人结算账户绑定我行或他行I类卡功能，以便实现绑定卡之间能够互相转账（充值、提现）等功能
 *
 * @author xcz
 */
public class SettlementAccountBindingTest {
    public static void main(String[] args) throws Exception {
        String sm4Key = GenerateKeyUtils.generateSM4Key(); //生成SM4密钥
        DefaultIcbcClient client = new DefaultIcbcClient(BaseData.APP_ID, BaseData.MY_RSA_PRIVATE_KEY, BaseData.ICBC_RSA_PUBLIC_KEY);
        SettlementAccountBindingRequestV1 request = new SettlementAccountBindingRequestV1();
        SettlementAccountBindingRequestV1.SettlementAccountBindingRequestV1Biz bizContent = new SettlementAccountBindingRequestV1.SettlementAccountBindingRequestV1Biz();
        request.setServiceUrl(BaseData.ACCOUNT_BINDING_URL);//API地址
        bizContent.setCorpNo(BaseData.APP_ID); //合作方机构编号
        bizContent.setTrxAccDate(BaseData.TEST_TRX_ACC_DATE); //合作方交易日期
        bizContent.setTrxAccTime(BaseData.TEST_TRX_ACC_TIME); //合作方交易时间
        bizContent.setCorpDate(BaseData.TEST_CORP_DATE); //合作方工作日期
        String corpSerNo = Util.getNonceStr(16);
        SaveFileUtils.setBindMedium(corpSerNo);//保存单号，短信验证/订单查询时使用
        bizContent.setCorpSerno(corpSerNo); //合作方交易单号
        bizContent.setOutServiceCode("binding"); //外部服务代码
        bizContent.setCorpMediumId(BaseData.TEST_CORP_MEDIUM_ID); //合作方介质号
        bizContent.setMediumId(SecretUtils.encryptSM4(sm4Key, BaseData.TEST_ACCOUNT_NUMBER_2)); //工行联名卡号
        bizContent.setBindMedium(SecretUtils.encryptSM4(sm4Key, BaseData.TEST_BINDING_ACCOUNT_NUMBER_1)); //绑定的I类卡号
        bizContent.setCertType(0); //证件类型
        bizContent.setCertNo(SecretUtils.encryptSM4(sm4Key, BaseData.TEST_CERT_NO)); //证件号码
        bizContent.setCustName(SecretUtils.encryptSM4(sm4Key, BaseData.TEST_CUS_NAME)); //户名
        bizContent.setMobileNo(SecretUtils.encryptSM4(sm4Key, BaseData.TEST_PHONE_NUMBER)); //手机号
        bizContent.setNotifyAddr(BaseData.TEST_NOTIFY_ADDR); //通知地址
        bizContent.setSecretKey(SecretUtils.encryptSM2(sm4Key)); //使用SM2加密的SM4对称密钥
        bizContent.setBindMediumHash(MD5Util.string2MD5(BaseData.TEST_BINDING_ACCOUNT_NUMBER_1)); //绑定的I类卡的HASH值
        bizContent.setMediumIdHash(MD5Util.string2MD5(BaseData.TEST_ACCOUNT_NUMBER_2)); //联名卡卡号哈希值
        request.setBizContent(bizContent);
        SettlementAccountBindingResponseV1 response = client.execute(request);
        System.out.println(JSON.toJSONString(response));
        if (response.isSuccess()) {
            SaveFileUtils.setSmsSendNo(response.getSmsSendNo());//保存短信验证码编号
        } else {
        }

    }
}
